//
//  PostUitls.m
//  CarTown
//
//  Created by xionghao on 15/8/13.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "PostUitls.h"
#import "NetWorkUtils.h"
#import "IWUploadParam.h"
@implementation PostUitls
+(void)Post:(NSString *)url parameters:(id)parameters success:(void (^)(id response))success failure:(void (^)(NSError *error))failure{
    NSMutableDictionary *dict=parameters;
    [NetWorkUtils getToken:^(NSString *tokenstr) {
        NSLog(@"%@",tokenstr);
        dict[@"token"]=tokenstr;
        [NetWorkUtils post:url parameters:dict success:^(id responseObject) {
            if (success) {
                success(responseObject);
            }
        } failure:^(NSError *error) {
            if (failure) {
                failure(error);
            }
        }];
    }];
}
+(void)upload:(NSString *)url parameters:(id)parameters uploadarray:(NSArray *)array success:(void (^)(id responseobj))success failure:(void (^)(NSError *error))failure{
    NSMutableDictionary *dict=parameters;
    [NetWorkUtils getToken:^(NSString *tokenstr) {
        dict[@"token"]=tokenstr;
        for (IWUploadParam *param in array) {
            NSString *base64str=[param.data imageBase64Data:param.data];
//            NSString *baseString = (__bridge NSString *) CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,                                                                                           (CFStringRef)base64str,
//                                                                                        NULL,
//                                                                                                 CFSTR(":/?#[]@!$&’()*+,;="),
//                                                                                                 kCFStringEncodingUTF8);
//            NSLog(@"%@",baseString);
            dict[param.paramName]=base64str;
        }
        [NetWorkUtils post:url parameters:dict success:^(id responseObject) {
            if (success) {
                success(responseObject);
            }
        } failure:^(NSError *error) {
            if (failure) {
                failure(error);
            }
        }];
        
    }];
}

- (BOOL) imageHasAlpha: (UIImage *) image
{
    CGImageAlphaInfo alpha = CGImageGetAlphaInfo(image.CGImage);
    return (alpha == kCGImageAlphaFirst ||
            alpha == kCGImageAlphaLast ||
            alpha == kCGImageAlphaPremultipliedFirst ||
            alpha == kCGImageAlphaPremultipliedLast);
}
- (NSString *) image2DataURL: (UIImage *) image
{
    NSData *imageData = nil;
    NSString *mimeType = nil;
    
    if ([self imageHasAlpha: image]) {
        imageData = UIImagePNGRepresentation(image);
        mimeType = @"image/png";
    } else {
        imageData = UIImageJPEGRepresentation(image, 1.0f);
        mimeType = @"image/jpeg";
    }
    
    return [NSString stringWithFormat:@"data:%@;base64,%@", mimeType,
            [imageData base64EncodedStringWithOptions: 0]];
    
}

@end
